<?php
/*
 +----------------------------------------------------------------------
 + Title        : TemplateMessageService 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/7/31 18:23
 + Last-time    : 2019/7/31 18:23+ 86187
 + Desc         : TemplateMessageService
 +----------------------------------------------------------------------
*/


namespace app\common\service;

use app\common\lib\Job;

/**
 * 模板消息推送
 * Class TemplateMessageService
 *
 * @package app\common\service
 */
class TemplateMessageService extends BaseService
{


    /**
     * 模板消息推送
     * @param        $type
     * @param        $data
     * @param string $url
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author : Randy_chen
     * @Date   : 2019/7/31
     * @Time   : 23:17
     */
    public function send($type, $data, $url = '')
    {
        $info = getTempInfo($type,true);
        $temp_message = $info['template_format'];
        $temp_arr = json_decode($temp_message, true);
        $temp_arr['template_id'] =  $info['template'];
//        $temp_arr = $this->getAd ($type , $temp_arr);
        $temp_arr['url'] = $url;
        $str = json_encode($temp_arr);
        $replace_arr = getValues($temp_arr);
        foreach ($replace_arr as $k => $v) {
            $str = str_replace($v, $data[$k], $str);
        }
        $add['openid'] = $data[0];
        $add['template_id'] = $info['id'];
        $add['message_body'] = $str;
        $add['ctime'] = date('Y-m-d H:i:s');
        //添加至消息队列
        $job = new Job();
        //加入模板消息推送队列
        $job->actionWithJob('send_temp_message' ,'temp_message' ,$add);
        return true;
    }

    public function getAd($type , $temp_arr){
        switch ($type){
            case 'CUST_SELL':
                $model = new AdModel();
                $info = $model->getInfo (2);
                $temp_arr['data']['remark']['value'] = $info['title'];
                break;
        }
        return $temp_arr;
    }

}